package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.CustomerQuery;
import cn.wolfcode.service.CustomerService;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.service.SystemDictionaryItemService;
import cn.wolfcode.vo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SystemDictionaryItemService itemService;

    @Autowired
    private EmployeeService employeeService;


    @RequiresPermissions(value = {"customer:potentialList", "客户管理页面"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        //设置查询潜在客户
        qo.setStatus(0L);
        if (!(SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("Market_Manager"))) {
            qo.setSellerId(((Employee) SecurityUtils.getSubject().getPrincipal()).getId());
        }
        PageInfo<Customer> pageInfo = customerService.query(qo);

        //查询客户工作列表
        List<SystemDictionaryItem> jobs = itemService.selectByItemSn("job");
        //查询客户来源列表
        List<SystemDictionaryItem> sources = itemService.selectByItemSn("source");
        //查询销售员列表
        String[] sns = {"Market_Manager", "Market"};
        List<Employee> sellers = employeeService.selectByRoleSn(sns);
        //查询交流方式列表
        List<SystemDictionaryItem> ccts = itemService.selectByItemSn("communicationMethod");


        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("jobs", jobs);
        model.addAttribute("sources", sources);
        model.addAttribute("sellers", sellers);
        model.addAttribute("ccts", ccts);
        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete", "客户管理删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate", "客户管理新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
        customerService.saveOrUpdate(customer);

        return new JsonResult();
    }
}
